let xk_data = []

if (uni.getStorageSync('questionDesc')) {
	xk_data = uni.getStorageSync('questionDesc').XK
}


// 学科ID 1:理科,2:文科,3:物理,4:化学,5:生物,6:政治,7:历史,8:地理,9:技术
const xk_ids = {
	'物理': 1,
	'化学': 2,
	'政治': 6,
	'历史': 4,
	'生物': 3,
	'地理': 5,
}

function getBaogaoXK(data) {
	const types = Object.keys(data);
	if (types.length != 9) {
		return;
	}

	let dataXK = [];

	// 转成数组
	types.forEach(type => {
		dataXK.push({
			't': type,
			'v': data[type],
			'd': xk_data[type],
		});
	});

	// 排序
	dataXK.sort((item1, item2) => {
		if (item1.v == item2.v) {
			return item2.d.s - item1.d.s;
		}
		return item2.v - item1.v;
	});

	const xk1 = dataXK[0];
	const xk2 = dataXK[1];
	const xk3 = dataXK[2];

	// 类型
	const xk_lx = `${xk1.d.n}+${xk2.d.n}+${xk3.d.n}`;

	const xk4 = dataXK[3];
	const xk5 = dataXK[4];
	const xk6 = dataXK[5];
	const xk7 = dataXK[6];
	const xk8 = dataXK[7];
	const xk9 = dataXK[8];

	// 排序
	dataXK.sort((item1, item2) => {
		return item1.d.s - item2.d.s;
	});

	// 九维
	const categories = [];
	const _data = [];
	dataXK.forEach(item => {
		categories.push(item.d.n);
		const v = 100 + 10 * item.v;
		_data.push(item.v);
	});
	const xk_jw = {
		"categories": categories,
		"series": [{
			"name": "你的学科优劣",
			"data": _data
		}]
	};

	// 每项
	const xk_mx = [];
	dataXK.forEach(item => {
		xk_mx.push({
			't': item.d.n,
			'c': (item.v > 5 ? item.d.y : item.d.r),
		})
	});

	return {
		"xk_lx": xk_lx,
		"xk_jw": xk_jw,
		"xk_mx": xk_mx,
	};
}

function getLastXK(data, xk_type) {
	const types = Object.keys(data);
	if (types.length != 9) {
		return;
	}

	let dataXK = [];

	// 转成数组
	types.forEach(type => {
		const d = xk_data[type];
		const sig = xk_ids[d.n];
		if (sig != undefined) {
			dataXK.push({
				't': type,
				'v': data[type],
				'sig': sig,
				'd': d,
			});
		}
	});

	// 排序
	dataXK.sort((item1, item2) => {
		if (item1.v == item2.v) {
			return item1.d.s - item2.d.s;
		}
		return item1.v - item2.v;
	});

	// 2 : 3(语数外) + 3(物理/历史/化学/生物/地理/政治)
	let xk1 = dataXK[0];
	let xk2 = dataXK[1];
	let xk3 = dataXK[2];

	// 3 : 3(语数外) + 1(物理/历史) + 2(化学/生物/地理/政治)
	if (xk_type == 3) {
		const _xk1 = dataXK.find(item => {
			return item.t == "WL" || item.t == "LS"
		});
		xk1 = _xk1;
		dataXK = dataXK.filter(item => {
			return item.t != "WL" && item.t != "LS"
		});
		xk2 = dataXK[0];
		xk3 = dataXK[1];
	}
	// 类型
	return `${xk1.sig},${xk2.sig},${xk3.sig}`;
}

export default {
	getBaogaoXK: getBaogaoXK,
	getLastXK: getLastXK,
}